/*     */ package cc.s2m.web.s2mBlog.controller;
/*     */ 
/*     */ import cc.s2m.open_all_open.weixin.WxRecieveMsg;
/*     */ import cc.s2m.web.s2mBlog.controller.base.BaseController;
/*     */ import cc.s2m.web.s2mBlog.pojo.Articles;
/*     */ import cc.s2m.web.s2mBlog.pojo.ArticlesTags;
/*     */ import cc.s2m.web.s2mBlog.pojo.SysConfig;
/*     */ import cc.s2m.web.s2mBlog.pojo.Tags;
/*     */ import cc.s2m.web.s2mBlog.pojo.WeixinAdmin;
/*     */ import cc.s2m.web.s2mBlog.service.IArticles;
/*     */ import cc.s2m.web.s2mBlog.service.IArticlesTags;
/*     */ import cc.s2m.web.s2mBlog.service.ISysConfig;
/*     */ import cc.s2m.web.s2mBlog.service.ITags;
/*     */ import cc.s2m.web.s2mBlog.service.IWeixinAdmin;
/*     */ import cc.s2m.web.s2mBlog.util.MemcacheKeys;
/*     */ import cc.s2m.web.s2mBlog.util.StaticProp;
/*     */ import cc.s2m.web.s2mBlog.vo.Expressions;
/*     */ import cc.s2m.web.s2mBlog.vo.VO;
/*     */ import com.google.common.base.Strings;
/*     */ import com.google.common.collect.Lists;
/*     */ import com.google.common.collect.Maps;
/*     */ import java.io.IOException;
/*     */ import java.io.PrintWriter;
/*     */ import java.util.HashMap;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import javax.servlet.http.HttpServletRequest;
/*     */ import javax.servlet.http.HttpServletResponse;
/*     */ import net.spy.memcached.MemcachedClient;
/*     */ import org.apache.logging.log4j.LogManager;
/*     */ import org.apache.logging.log4j.Logger;
/*     */ import org.springframework.beans.factory.annotation.Autowired;
/*     */ import org.springframework.stereotype.Controller;
/*     */ import org.springframework.web.bind.annotation.RequestMapping;
/*     */ 
/*     */ @Controller
/*     */ public class Weixin extends BaseController
/*     */ {
/*  40 */   private static final Logger LOGGER = LogManager.getLogger(Weixin.class);
/*     */ 
/*     */   @Autowired
/*     */   private ISysConfig sysConfigService;
/*     */ 
/*     */   @Autowired
/*     */   private IArticles articlesService;
/*     */ 
/*     */   @Autowired
/*     */   private ITags tagsService;
/*     */ 
/*     */   @Autowired
/*     */   private IArticlesTags articlesTagsService;
/*     */ 
/*     */   @Autowired
/*     */   private IWeixinAdmin weixinAdminService;
/*     */ 
/*     */   @Autowired
/*     */   private MemcachedClient memcachedClient;
/*     */ 
/*  64 */   @RequestMapping({"/weixin"})
/*     */   public void weixin(HttpServletRequest request, HttpServletResponse response, String signature, String timestamp, String nonce, String echostr) throws IOException { boolean isCheckReal = WxRecieveMsg.checkIsReal(StaticProp.getSysConfigString("weixin.token"), signature, timestamp, nonce);
/*     */ 
/*  69 */     if (!isCheckReal) {
/*  70 */       response.setContentType("text/plain;charset=UTF-8");
/*  71 */       response.setCharacterEncoding("UTF-8");
/*  72 */       response.getWriter().print("signatureError");
/*  73 */       return;
/*     */     }
/*     */ 
/*  76 */     if (!Strings.isNullOrEmpty(echostr)) {
/*  77 */       response.setContentType("text/plain;charset=UTF-8");
/*  78 */       response.setCharacterEncoding("UTF-8");
/*  79 */       response.getWriter().print(echostr);
/*  80 */       return;
/*     */     }
/*     */ 
/*  83 */     Map message = WxRecieveMsg.getWeixinPushData(request);
/*  84 */     String msgType = (String)message.get("MsgType");
/*  85 */     if (Strings.isNullOrEmpty(msgType)) {
/*  86 */       response.setContentType("text/plain;charset=UTF-8");
/*  87 */       response.setCharacterEncoding("UTF-8");
/*  88 */       response.getWriter().print("msgTypeError");
/*  89 */       return;
/*     */     }
/*  91 */     switch (msgType)
/*     */     {
/*     */     case "event":
/*  94 */       String event = (String)message.get("Event");
/*  95 */       if (event.equals("subscribe")) { StringBuffer buffer = new StringBuffer();
/*  97 */         buffer.append("<xml>");
/*  98 */         buffer.append("<ToUserName><![CDATA[" + (String)message.get("FromUserName") + "]]></ToUserName>");
/*  99 */         buffer.append("<FromUserName><![CDATA[" + (String)message.get("ToUserName") + "]]></FromUserName>");
/* 100 */         buffer.append("<CreateTime>" + (String)message.get("CreateTime") + "</CreateTime>");
/* 101 */         buffer.append("<MsgType><![CDATA[text]]></MsgType>");
/* 102 */         buffer.append("<Content><![CDATA[" + this.sysConfigService.getByCode("WEIXIN_WELCOME").getValue() + "]]></Content>");
/* 103 */         buffer.append("</xml>");
/*     */ 
/* 105 */         response.setContentType("text/xml;charset=UTF-8");
/* 106 */         response.setCharacterEncoding("UTF-8");
/* 107 */         response.getWriter().print(buffer.toString());
/*     */         return; } break;
/*     */     case "text":
/* 113 */       String content = (String)message.get("Content");
/* 114 */       if (Strings.isNullOrEmpty(content)) {
/* 115 */         response.setContentType("text/plain;charset=UTF-8");
/* 116 */         response.setCharacterEncoding("UTF-8");
/* 117 */         response.getWriter().print("");
/* 118 */         return;
/*     */       }
/* 120 */       content = content.trim();
/*     */       StringBuffer buffer;
/* 121 */       switch (content) {
/*     */       case "?":
/*     */       case "？":
/* 124 */         buffer = new StringBuffer();
/* 125 */         buffer.append("<xml>");
/* 126 */         buffer.append("<ToUserName><![CDATA[" + (String)message.get("FromUserName") + "]]></ToUserName>");
/* 127 */         buffer.append("<FromUserName><![CDATA[" + (String)message.get("ToUserName") + "]]></FromUserName>");
/* 128 */         buffer.append("<CreateTime>" + (String)message.get("CreateTime") + "</CreateTime>");
/* 129 */         buffer.append("<MsgType><![CDATA[text]]></MsgType>");
/* 130 */         buffer.append("<Content><![CDATA[" + this.sysConfigService.getByCode("WEIXIN_WELCOME").getValue() + "]]></Content>");
/* 131 */         buffer.append("</xml>");
/*     */ 
/* 133 */         response.setContentType("text/xml;charset=UTF-8");
/* 134 */         response.setCharacterEncoding("UTF-8");
/* 135 */         response.getWriter().print(buffer.toString());
/* 136 */         return;
/*     */       case "top1":
/*     */       case "top2":
/*     */       case "top3":
/*     */       case "top4":
/*     */       case "top5":
/*     */       case "top6":
/*     */       case "top7":
/*     */       case "top8":
/*     */       case "top9":
/*     */       case "top10":
/* 147 */         int number = Integer.parseInt(content.substring(3));
/* 148 */         Map map = new HashMap();
/* 149 */         map.put("pageSize", Integer.valueOf(number));
/* 150 */         List<Articles> list = this.articlesService.getList(new Articles(), map);
/* 151 */         buffer = new StringBuffer();
/* 152 */         buffer.append("<xml>");
/* 153 */         buffer.append("<ToUserName><![CDATA[" + (String)message.get("FromUserName") + "]]></ToUserName>");
/* 154 */         buffer.append("<FromUserName><![CDATA[" + (String)message.get("ToUserName") + "]]></FromUserName>");
/* 155 */         buffer.append("<CreateTime>" + (String)message.get("CreateTime") + "</CreateTime>");
/* 156 */         buffer.append("<MsgType><![CDATA[news]]></MsgType>");
/* 157 */         buffer.append("<ArticleCount>" + list.size() + "</ArticleCount>");
/* 158 */         buffer.append("<Articles>");
/* 159 */         for (Articles bean : list) {
/* 160 */           buffer.append("<item>");
/* 161 */           buffer.append("<Title><![CDATA[" + bean.getTitle() + "]]></Title>");
/* 162 */           buffer.append("<Description><![CDATA[" + bean.getProfile() + "]]></Description>");
/* 163 */           if (!Strings.isNullOrEmpty(bean.getPic())) {
/* 164 */             buffer.append("<PicUrl><![CDATA[" + bean.getPic() + "]]></PicUrl>");
/*     */           }
/* 166 */           buffer.append("<Url><![CDATA[http://www.s2m.cc/blog/" + bean.getUrl() + "/]]></Url>");
/* 167 */           buffer.append("</item>");
/*     */         }
/* 169 */         buffer.append("</Articles>");
/* 170 */         buffer.append("</xml>");
/*     */ 
/* 172 */         response.setContentType("text/xml;charset=UTF-8");
/* 173 */         response.setCharacterEncoding("UTF-8");
/* 174 */         response.getWriter().print(buffer.toString());
/* 175 */         return;
/*     */       }
/*     */ 
/* 178 */       String memWeixinAdminCode = (String)this.memcachedClient.get(MemcacheKeys.WEIXIN_ADMIN_TOKEN.getKey());
/* 179 */       if ((memWeixinAdminCode != null) && (memWeixinAdminCode.equals(content))) {
/* 180 */         WeixinAdmin weixinAdmin = new WeixinAdmin();
/* 181 */         weixinAdmin.setOpenid((String)message.get("FromUserName"));
/* 182 */         this.weixinAdminService.insertSelective(weixinAdmin);
/* 183 */         this.memcachedClient.delete(MemcacheKeys.WEIXIN_ADMIN_TOKEN.getKey());
/*     */ 
/* 185 */         buffer = new StringBuffer();
/* 186 */         buffer.append("<xml>");
/* 187 */         buffer.append("<ToUserName><![CDATA[" + (String)message.get("FromUserName") + "]]></ToUserName>");
/* 188 */         buffer.append("<FromUserName><![CDATA[" + (String)message.get("ToUserName") + "]]></FromUserName>");
/* 189 */         buffer.append("<CreateTime>" + (String)message.get("CreateTime") + "</CreateTime>");
/* 190 */         buffer.append("<MsgType><![CDATA[text]]></MsgType>");
/* 191 */         buffer.append("<Content><![CDATA[您已经成为微信管理员]]></Content>");
/* 192 */         buffer.append("</xml>");
/*     */ 
/* 194 */         response.setContentType("text/xml;charset=UTF-8");
/* 195 */         response.setCharacterEncoding("UTF-8");
/* 196 */         response.getWriter().print(buffer.toString());
/* 197 */         return;
/*     */       }
/*     */ 
/* 200 */       if (content.equalsIgnoreCase("q")) {
/* 201 */         String openid = (String)message.get("FromUserName");
/* 202 */         List resources = (List)this.memcachedClient.get(MemcacheKeys.WEIXIN_RESOURCES.getKey() + openid);
/* 203 */         if ((resources != null) && (!resources.isEmpty()))
/*     */         {
/* 205 */           List openIds = (List)this.memcachedClient.get(MemcacheKeys.WEIXIN_RESOURCES.getKey() + "openids");
/* 206 */           if ((openIds == null) || (openIds.isEmpty())) {
/* 207 */             openIds = Lists.newArrayList();
/*     */           }
/* 209 */           if (!openIds.contains(openid)) {
/* 210 */             openIds.add(openid);
/*     */           }
/* 212 */           this.memcachedClient.set(MemcacheKeys.WEIXIN_RESOURCES.getKey() + "openids", 86400, openIds);
/*     */ 
/* 214 */           buffer = new StringBuffer();
/* 215 */           buffer.append("<xml>");
/* 216 */           buffer.append("<ToUserName><![CDATA[" + (String)message.get("FromUserName") + "]]></ToUserName>");
/* 217 */           buffer.append("<FromUserName><![CDATA[" + (String)message.get("ToUserName") + "]]></FromUserName>");
/* 218 */           buffer.append("<CreateTime>" + (String)message.get("CreateTime") + "</CreateTime>");
/* 219 */           buffer.append("<MsgType><![CDATA[text]]></MsgType>");
/* 220 */           buffer.append("<Content><![CDATA[正在为您发布，稍等片刻，回复 top1 ，获取博文。]]></Content>");
/* 221 */           buffer.append("</xml>");
/*     */ 
/* 223 */           response.setContentType("text/xml;charset=UTF-8");
/* 224 */           response.setCharacterEncoding("UTF-8");
/* 225 */           response.getWriter().print(buffer.toString());
/* 226 */           return;
/*     */         }
/*     */       }
/*     */ 
/* 230 */       Map map = new HashMap();
/* 231 */       VO vo = new VO();
/* 232 */       vo.and(Expressions.like("title", "%" + content + "%"));
/* 233 */       Tags tagBean = new Tags();
/* 234 */       tagBean.setName(content);
/* 235 */       tagBean = (Tags)this.tagsService.getByCondition(tagBean);
/* 236 */       if (tagBean != null)
/*     */       {
/* 238 */         List aids = Lists.newArrayList();
/* 239 */         ArticlesTags condition = new ArticlesTags();
/* 240 */         condition.setTid(tagBean.getId());
/* 241 */         List<ArticlesTags> tagArticles = this.articlesTagsService.getList(condition, null);
/* 242 */         for (ArticlesTags atag : tagArticles) {
/* 243 */           aids.add(atag.getAid());
/*     */         }
/* 245 */         if (!tagArticles.isEmpty())
/*     */         {
/* 247 */           vo.or(Expressions.in("id", aids));
/*     */         }
/*     */       }
/* 250 */       map.put("vo", vo);
/* 251 */       map.put("pageSize", Integer.valueOf(10));
/* 252 */       List<Articles> list = this.articlesService.getList(new Articles(), map);
/* 253 */       if (list.isEmpty()) {
/* 254 */         buffer = new StringBuffer();
/* 255 */         buffer.append("<xml>");
/* 256 */         buffer.append("<ToUserName><![CDATA[" + (String)message.get("FromUserName") + "]]></ToUserName>");
/* 257 */         buffer.append("<FromUserName><![CDATA[" + (String)message.get("ToUserName") + "]]></FromUserName>");
/* 258 */         buffer.append("<CreateTime>" + (String)message.get("CreateTime") + "</CreateTime>");
/* 259 */         buffer.append("<MsgType><![CDATA[text]]></MsgType>");
/* 260 */         buffer.append("<Content><![CDATA[暂无“" + content + "”的相关记录]]></Content>");
/* 261 */         buffer.append("</xml>");
/*     */ 
/* 263 */         response.setContentType("text/xml;charset=UTF-8");
/* 264 */         response.setCharacterEncoding("UTF-8");
/* 265 */         response.getWriter().print(buffer.toString());
/* 266 */         return;
/*     */       }
/* 268 */       buffer = new StringBuffer();
/* 269 */       buffer.append("<xml>");
/* 270 */       buffer.append("<ToUserName><![CDATA[" + (String)message.get("FromUserName") + "]]></ToUserName>");
/* 271 */       buffer.append("<FromUserName><![CDATA[" + (String)message.get("ToUserName") + "]]></FromUserName>");
/* 272 */       buffer.append("<CreateTime>" + (String)message.get("CreateTime") + "</CreateTime>");
/* 273 */       buffer.append("<MsgType><![CDATA[news]]></MsgType>");
/* 274 */       buffer.append("<ArticleCount>" + list.size() + "</ArticleCount>");
/* 275 */       buffer.append("<Articles>");
/* 276 */       for (Articles bean : list) {
/* 277 */         buffer.append("<item>");
/* 278 */         buffer.append("<Title><![CDATA[" + bean.getTitle() + "]]></Title>");
/* 279 */         buffer.append("<Description><![CDATA[" + bean.getProfile() + "]]></Description>");
/* 280 */         if (!Strings.isNullOrEmpty(bean.getPic())) {
/* 281 */           buffer.append("<PicUrl><![CDATA[" + bean.getPic() + "]]></PicUrl>");
/*     */         }
/* 283 */         buffer.append("<Url><![CDATA[http://www.s2m.cc/blog/" + bean.getUrl() + "/]]></Url>");
/* 284 */         buffer.append("</item>");
/*     */       }
/* 286 */       buffer.append("</Articles>");
/* 287 */       buffer.append("</xml>");
/*     */ 
/* 289 */       response.setContentType("text/xml;charset=UTF-8");
/* 290 */       response.setCharacterEncoding("UTF-8");
/* 291 */       response.getWriter().print(buffer.toString());
/* 292 */       return;
/*     */     case "image":
/*     */     case "video":
/*     */     case "shortvideo":
/* 297 */       String openid = (String)message.get("FromUserName");
/* 298 */       WeixinAdmin weixinAdmin = new WeixinAdmin();
/* 299 */       weixinAdmin.setOpenid(openid);
/* 300 */       weixinAdmin = (WeixinAdmin)this.weixinAdminService.getByCondition(weixinAdmin);
/* 301 */       if (weixinAdmin == null) {
/* 302 */         buffer = new StringBuffer();
/* 303 */         buffer.append("<xml>");
/* 304 */         buffer.append("<ToUserName><![CDATA[" + (String)message.get("FromUserName") + "]]></ToUserName>");
/* 305 */         buffer.append("<FromUserName><![CDATA[" + (String)message.get("ToUserName") + "]]></FromUserName>");
/* 306 */         buffer.append("<CreateTime>" + (String)message.get("CreateTime") + "</CreateTime>");
/* 307 */         buffer.append("<MsgType><![CDATA[text]]></MsgType>");
/* 308 */         buffer.append("<Content><![CDATA[您还未获得分享微博权限，请联系管理员为您开通。]]></Content>");
/* 309 */         buffer.append("</xml>");
/*     */ 
/* 311 */         response.setContentType("text/xml;charset=UTF-8");
/* 312 */         response.setCharacterEncoding("UTF-8");
/* 313 */         response.getWriter().print(buffer.toString());
/* 314 */         return;
/*     */       }
/* 316 */       List resources = (List)this.memcachedClient.get(MemcacheKeys.WEIXIN_RESOURCES.getKey() + openid);
/* 317 */       if ((resources == null) || (resources.isEmpty())) {
/* 318 */         resources = Lists.newArrayList();
/*     */       }
/*     */ 
/* 321 */       Map resource = Maps.newHashMap();
/* 322 */       resource.put("type", msgType);
/* 323 */       resource.put("mediaId", message.get("MediaId"));
/* 324 */       resources.add(resource);
/* 325 */       this.memcachedClient.set(MemcacheKeys.WEIXIN_RESOURCES.getKey() + openid, 86400, resources);
/*     */ 
/* 328 */       buffer = new StringBuffer();
/* 329 */       buffer.append("<xml>");
/* 330 */       buffer.append("<ToUserName><![CDATA[" + (String)message.get("FromUserName") + "]]></ToUserName>");
/* 331 */       buffer.append("<FromUserName><![CDATA[" + (String)message.get("ToUserName") + "]]></FromUserName>");
/* 332 */       buffer.append("<CreateTime>" + (String)message.get("CreateTime") + "</CreateTime>");
/* 333 */       buffer.append("<MsgType><![CDATA[text]]></MsgType>");
/* 334 */       buffer.append("<Content><![CDATA[上传成功 " + resources.size() + " 个素材，还有素材请继续上传，结束上传请回复q]]></Content>");
/* 335 */       buffer.append("</xml>");
/*     */ 
/* 337 */       response.setContentType("text/xml;charset=UTF-8");
/* 338 */       response.setCharacterEncoding("UTF-8");
/* 339 */       response.getWriter().print(buffer.toString());
/* 340 */       return;
/*     */     default:
/* 342 */       LOGGER.error("错误的MsgType类型：{}", new Object[] { msgType });
/*     */     }
/*     */ 
/* 345 */     response.setContentType("text/plain;charset=UTF-8");
/* 346 */     response.setCharacterEncoding("UTF-8");
/* 347 */     response.getWriter().print("");
/*     */   }
/*     */ }

/* Location:           C:\Users\Administrator\Desktop\s2mBlog.zip
 * Qualified Name:     s2mBlog-3.0.0.WEB-INF.classes.cc.s2m.web.s2mBlog.controller.Weixin
 * JD-Core Version:    0.6.1
 */